//*******************************************************************************
// COPYRIGHT NOTES
// ---------------
// This is a sample for BCGControlBar Library Professional Edition
// Copyright (C) 1998-2016 BCGSoft Ltd.
// All rights reserved.
//
// This source code can be used, distributed or modified
// only under terms and conditions 
// of the accompanying license agreement.
//*******************************************************************************
//
// ChartPieView.cpp : implementation file
//

#include "stdafx.h"
#include "bcgpchartexample.h"
#include "ChartPieView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CChartPieView

IMPLEMENT_DYNCREATE(CChartPieView, CBCGPChartExampleView)

CChartPieView::CChartPieView()
	: CBCGPChartExampleView(CChartPieView::IDD)
{
	//{{AFX_DATA_INIT(CChartPieView)
	m_nFillGradientType = 5;
	m_bShowDataLabels = TRUE;
	m_nDataLabelPosition = 0;
	m_bExplodePie = FALSE;
	m_bFitDiagramArea = FALSE;
	m_nHoleSize = 50;
	m_strHoleSize = _T("");
	m_bShowIndexInLabel = FALSE;
	//}}AFX_DATA_INIT

	m_strHoleSize.Format(_T("%d%%"), m_nHoleSize);
}

CChartPieView::~CChartPieView()
{
}

void CChartPieView::DoDataExchange(CDataExchange* pDX)
{
	CBCGPChartExampleView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CChartPieView)
	DDX_Control(pDX, IDC_ROTATE, m_wndRotate);
	DDX_Control(pDX, IDC_FIT_DIAGRAM_AREA, m_wndFitDiagramArea);
	DDX_Control(pDX, IDC_FILL_GRADIENT_TYPE, m_wndFillGradientType);
	DDX_Control(pDX, IDC_LABEL_DOUGHNUT, m_wndDoughnutLabel);
	DDX_Control(pDX, IDC_DOUGHNUT_VALUE, m_wndHoleSizeValue);
	DDX_Control(pDX, IDC_DOUGHNUT_SIZE, m_wndHoleSize);
	DDX_Control(pDX, IDC_DATA_LABEL_POSITION, m_wndDataLabelPosition);
	DDX_Control(pDX, IDC_CHART, m_wndChart);
	DDX_CBIndex(pDX, IDC_FILL_GRADIENT_TYPE, m_nFillGradientType);
	DDX_Check(pDX, IDC_DATA_LABELS, m_bShowDataLabels);
	DDX_CBIndex(pDX, IDC_DATA_LABEL_POSITION, m_nDataLabelPosition);
	DDX_Check(pDX, IDC_EXPLODED_PIE, m_bExplodePie);
	DDX_Check(pDX, IDC_FIT_DIAGRAM_AREA, m_bFitDiagramArea);
	DDX_Slider(pDX, IDC_DOUGHNUT_SIZE, m_nHoleSize);
	DDX_Text(pDX, IDC_DOUGHNUT_VALUE, m_strHoleSize);
	DDX_Check(pDX, IDC_SHOW_DP_INDEX_IN_LABEL, m_bShowIndexInLabel);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CChartPieView, CBCGPChartExampleView)
	//{{AFX_MSG_MAP(CChartPieView)
	ON_BN_CLICKED(IDC_DATA_LABELS, OnUpdateChart)
	ON_WM_HSCROLL()
	ON_BN_CLICKED(IDC_ROTATE, OnRotate)
	ON_CBN_SELENDOK(IDC_DATA_LABEL_POSITION, OnUpdateChart)
	ON_CBN_SELENDOK(IDC_FILL_GRADIENT_TYPE, OnUpdateChart)
	ON_BN_CLICKED(IDC_FIT_DIAGRAM_AREA, OnUpdateChart)
	ON_BN_CLICKED(IDC_EXPLODED_PIE, OnUpdateChart)
	ON_BN_CLICKED(IDC_SHOW_DP_INDEX_IN_LABEL, OnUpdateChart)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChartPieView diagnostics

#ifdef _DEBUG
void CChartPieView::AssertValid() const
{
	CBCGPChartExampleView::AssertValid();
}

void CChartPieView::Dump(CDumpContext& dc) const
{
	CBCGPChartExampleView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CChartPieView message handlers

void CChartPieView::OnInitialUpdate() 
{
	CBCGPChartExampleView::OnInitialUpdate();
	
	if (m_bIsReady)
	{
		return;
	}

	CBCGPInfoTipOptions infoTipOptions;
	infoTipOptions.m_StemLocation = CBCGPPopupWindow::BCGPPopupWindowStemLocation_Left;

	m_wndChart.EnableInfoTip(TRUE, &infoTipOptions);

	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	BOOL bIsPie = TRUE;

	switch (m_Feature)
	{
	case CDemoFeature::BCGP_Pie:
		pChart->SetChartType(BCGPChartPie);
		break;

	case CDemoFeature::BCGP_Pie3D:
		pChart->SetChartType(BCGPChartPie3D);
		break;

	case CDemoFeature::BCGP_Doughnut:
		pChart->SetChartType(BCGPChartDoughnut);
		bIsPie = FALSE;
		break;

	case CDemoFeature::BCGP_Doughnut3D:
		pChart->SetChartType(BCGPChartDoughnut3D);
		bIsPie = FALSE;
		break;

	case CDemoFeature::BCGP_Torus3D:
		pChart->SetChartType(BCGPChartTorus3D);
		break;
	}

	if (bIsPie)
	{
		m_wndDoughnutLabel.ShowWindow(SW_HIDE);
		m_wndHoleSizeValue.ShowWindow(SW_HIDE);
		m_wndHoleSize.ShowWindow(SW_HIDE);
		m_wndHoleSize.EnableWindow(FALSE);
	}

	m_wndRotate.GetRotationObject()->SetAutorepeatMode(100);
	m_wndRotate.GetRotationObject()->SetColorTheme(CBCGPRotationObject::BCGP_COLOR_THEME_VISUAL_MANAGER);
	m_wndRotate.GetRotationObject()->EnableFlatIcons();

	pChart->SetChartTitle(_T("Site visitors from US:"));

	pChart->AddChartData(_T("California"), 6700);
 	pChart->AddChartData(_T("Virginia"), 4500);
 	pChart->AddChartData(_T("New York"), 2200);
 	pChart->AddChartData(_T("Pennsylvania"), 1500);
 	pChart->AddChartData(_T("Georgia"), 1400);
 	pChart->AddChartData(_T("Colorado"), 1350);
 	pChart->AddChartData(_T("Other States"), 1700);

	m_wndHoleSize.SetRange(0, 90, TRUE);
	m_wndHoleSize.SetPos(m_nHoleSize);

	m_wndFillGradientType.EnableWindow(m_Feature != CDemoFeature::BCGP_Torus3D && !m_bIsTexturedTheme && !m_bIsFlatTheme);
	m_wndFitDiagramArea.EnableWindow(m_Feature != CDemoFeature::BCGP_Torus3D);

	m_bIsReady = TRUE;

	OnUpdateChart();
}

void CChartPieView::OnUpdateChart() 
{
	UpdateData();

	m_wndDataLabelPosition.EnableWindow(m_bShowDataLabels);

	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	pChart->ShowDataLabels(m_bShowDataLabels);

	if (m_bShowDataLabels)
	{
		BCGPChartDataLabelOptions dataLabelOptions = pChart->GetDataLabelOptions();
		dataLabelOptions.m_position = (BCGPChartDataLabelOptions::LabelPosition)m_nDataLabelPosition;
		dataLabelOptions.m_bUnderlineDataLabel = TRUE;
		dataLabelOptions.m_content = m_bShowIndexInLabel ? BCGPChartDataLabelOptions::LC_DP_INDEX : BCGPChartDataLabelOptions::LC_PIE_NAME_PERCENTAGE;
		dataLabelOptions.m_bDrawDataLabelBorder = dataLabelOptions.m_position != BCGPChartDataLabelOptions::LP_DEFAULT_POS && dataLabelOptions.m_position != BCGPChartDataLabelOptions::LP_OUTSIDE_END;

		pChart->SetDataLabelsOptions(dataLabelOptions);
	}

	CBCGPBrush::BCGP_GRADIENT_TYPE type;

	switch (m_nFillGradientType)
	{
	case 0:
	default:
		type = (CBCGPBrush::BCGP_GRADIENT_TYPE)-1;
		break;

	case 1:
		type = CBCGPBrush::BCGP_NO_GRADIENT;
		break;

	case 2:
		type = CBCGPBrush::BCGP_GRADIENT_DIAGONAL_LEFT;
		break;

	case 3:
		type = CBCGPBrush::BCGP_GRADIENT_DIAGONAL_RIGHT;
		break;

	case 4:
		type = CBCGPBrush::BCGP_GRADIENT_RADIAL_CENTER;
		break;

	case 5:
		type = CBCGPBrush::BCGP_GRADIENT_BEVEL;
		break;
	}

	for (int i = 0; i < pChart->GetSeriesCount(); i++)
	{
		CBCGPChartPieSeries* pSeries = DYNAMIC_DOWNCAST(CBCGPChartPieSeries, pChart->GetSeries(i));
		if (pSeries != NULL)
		{
			ASSERT_VALID(pSeries);

			pSeries->SetLegendLabelContent(m_bShowIndexInLabel ?
				(BCGPChartDataLabelOptions::LabelContent)(BCGPChartDataLabelOptions::LC_PIE_NAME_PERCENTAGE | BCGPChartDataLabelOptions::LC_DP_INDEX) :
				BCGPChartDataLabelOptions::LC_DEFAULT_CONTENT);

			pSeries->SetDefaultFillGradientType(type);
			pSeries->EnableFitDiagramArea(m_bFitDiagramArea);

			pSeries->SetPieExplosion(m_bExplodePie ? 10 : 0);
			pSeries->SetDataPointPieExplosion(m_bExplodePie ? 15 : 0, 0);

			CBCGPChartDoughnutSeries* pDoughnutSeries = DYNAMIC_DOWNCAST(CBCGPChartDoughnutSeries, pSeries);
			if (pDoughnutSeries != NULL)
			{
				ASSERT_VALID(pDoughnutSeries);

				pDoughnutSeries->SetDoughnutPercent(m_nHoleSize);
			}
		}
	}

	UpdateRotationButtons();

	pChart->Redraw();
}

void CChartPieView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	CBCGPChartExampleView::OnHScroll(nSBCode, nPos, pScrollBar);

	if (pScrollBar != NULL)
	{
		OnUpdateChart();

		if (m_wndHoleSize.IsWindowVisible())
		{
			m_strHoleSize.Format(_T("%d%%"), m_nHoleSize);
			UpdateData(FALSE);
		}
	}
}

void CChartPieView::OnRotate() 
{
	UpdateData();

	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	int xDelta = 10;
	double yDelta = 10.;

	for (int i = 0; i < pChart->GetSeriesCount(); i++)
	{
		CBCGPChartPieSeries* pSeries = DYNAMIC_DOWNCAST(CBCGPChartPieSeries, pChart->GetSeries(i));
		ASSERT_VALID(pSeries);

		int nRotationAngle = pSeries->GetPieRotation();
		double dblPieAngle = pSeries->GetPieAngle();

		switch (m_wndRotate.GetRotationObject()->GetClicked())
		{
		case CBCGPRotationObject::BCGP_ROTATION_UP:
			dblPieAngle -= yDelta;
			if (dblPieAngle < 1.)
			{
				dblPieAngle = 1.;
			}
			break;

		case CBCGPRotationObject::BCGP_ROTATION_DOWN:
			dblPieAngle += yDelta;
			break;

		case CBCGPRotationObject::BCGP_ROTATION_LEFT:
			nRotationAngle -= xDelta;
			break;

		case CBCGPRotationObject::BCGP_ROTATION_RIGHT:
			nRotationAngle += xDelta;
			break;

		case CBCGPRotationObject::BCGP_ROTATION_RESET:
			nRotationAngle = 0;
			dblPieAngle = 45.;
			break;
		}

		pSeries->SetPieRotation(nRotationAngle);
		pSeries->SetPieAngle(dblPieAngle);
	}
	
	pChart->SetDirty();
	pChart->Redraw();
}

void CChartPieView::UpdateRotationButtons()
{
	BOOL bIsUpDown = (m_Feature == CDemoFeature::BCGP_Pie3D || m_Feature == CDemoFeature::BCGP_Doughnut3D) && !m_bFitDiagramArea;

	CBCGPRotationObject* pRO = m_wndRotate.GetRotationObject();
	ASSERT_VALID(pRO);

	pRO->EnablePart(CBCGPRotationObject::BCGP_ROTATION_CLOCKWISE, FALSE);
	pRO->EnablePart(CBCGPRotationObject::BCGP_ROTATION_NARROW_FIELD_OF_VIEW, FALSE);
	pRO->EnablePart(CBCGPRotationObject::BCGP_ROTATION_WIDEN_FIELD_OF_VIEW, FALSE);
	pRO->EnablePart(CBCGPRotationObject::BCGP_ROTATION_COUNTER_CLOCKWISE, FALSE);

	pRO->EnablePart(CBCGPRotationObject::BCGP_ROTATION_UP, bIsUpDown);
	pRO->EnablePart(CBCGPRotationObject::BCGP_ROTATION_DOWN, bIsUpDown);

	pRO->Redraw();
}
